package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class DeviceImpl {

    public DeviceImpl() {
    //
    }

    public String getFullId(String id) {
        return "F:/TCC/forms/" + id + ".xml";
    }

    public String getFunctions() {
        return getContentFile(new File(getFullId("functions")));
    }

    public String getForm(String functionId) {
        if (functionId.equals("FNC1")) {
            return getContentFile(new File(getFullId("FormShutDown")));
        }
        if (functionId.equals("FNC2")) {
            return getContentFile(new File(getFullId("FormAlert")));
        }
        return null;
    }

    public String executeCommand(String command) {
        if (command.equals("getFunctions")) {
            return getFunctions();
        }
        if (command.startsWith("getForm")) {
            //o formato deve ser getForm:functionID
            final String functionID = command.split(":")[1];
            return getForm(functionID);
        }
        if (command.startsWith("execFuntion")) {
            //o formato deve ser execFuntion:formID:action
            String[] split = command.split(":");
            final String functionID = command.split(":")[1];
            StringBuilder sb = new StringBuilder();
            sb.append("BOF\n");
            sb.append(executeFunction(split[1], split[2]));
            sb.append("\nEOF");
            return sb.toString();
        }

        return "Command not found";
    }

    public String executeFunction(String formID, String action) {
        if (formID.equals("FormFc1")) {
            if (action.equals("Bt1")) {
                shutdown();
                return "OK";
            }
            if (action.equals("Bt2")) {
                hibernate();
                return "OK";
            }
        }
        if (formID.equals("FormFc2")) {
            if (action.equals("Bt1")) {
                alert("Rango na mesa");
                return "OK";
            }
            if (action.equals("Bt2")) {
                alert("Telefone");
                return "OK";
            }
            if (action.equals("Bt3")) {
                alert("Trovoada");
                return "OK";
            }
        }

        return "FAIL";
    }

    private void alert(String message) {
        System.out.println("!!!!!ALERTA!!!!!");
        System.out.println(message);
    }

    private void hibernate() {
        System.out.println("Hibenando");
    }

    private void shutdown() {
        System.out.println("Desligando");
    }

    private static String getContentFile(File file) {
        StringBuilder sb = new StringBuilder();
        sb.append("BOF\n");
        try {
            FileReader in = new FileReader(file);
            BufferedReader reader = new BufferedReader(in);

            while (reader.ready()) {
                sb.append(reader.readLine());
                sb.append("\n");
            }

            reader.close();
            in.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        sb.append("EOF");
        return sb.toString();
    }
}
